<?php 
class Upload{
	public $error;
	private $allowType;
	public function __construct($allowType=array('.jpg','.png','.gif','.bmp')){
		$this->allowType = $allowType;
	}
	public function up(){
		//重组数组
		$arr = $this->resetArr();
		//筛选
		foreach($arr as $v){
			if(!$this->filter($v)){
				return false;
			}
		}
		//上传

		foreach($arr as $v){
			
			$this-> move($v);

		}
	}
	//重新组成数组的方法
	private function resetArr(){
		$arr = array();
		$v = current($_FILES);
		if(is_array($v['name'])){
			foreach($v['name'] as $key =>$value){
				$arr[] = array(
					'name' => $value,
					'type' =>$v['type'][$key],
					'tmp_name' => $v['tmp_name'][$key],
					'error' => $v['error'][$key],
					'size' =>$v['size'][$key],
				);
			}
		}else{
			$arr[] = $v;
		}	
		return $arr;
	}
	//筛选方法
	private function filter($v){
		$type = strrchr($v['name'], '.');
		switch (true) {
			//没有文件被上传
			case $v['error']==4:
				$this->error= '没有文件被上传';
				return false;
				break;
			case !is_uploaded_file($v['tmp_name']):
				$this->error= '必须是合法的上传文件';
				return false;
				break;
			case !in_array($type,$this->allowType):
				$this->error= '类型不允许';
				return false;
				break;
			case $v['size']>22000000:
				$this->error= '文件超过配置大小';
				return false;
				break;
			case $v['error'] == 1:
				$this->error= '大小超过了 php.ini 中 upload_max_filesize 限制值';
				return false;
				break;
			case $v['error'] == 2:
				$this->error= '大小超过 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
				return false;
				break;
			case $v['error'] == 3:
				$this->error= '文件只有部分被上传';
				return false;
				break;
			default:
				return true;
				break;
		}
	}
	//上传方法
	private function move($v){
		$type = strrchr($v['name'],'.');
		$fileName = time().mt_rand(10000, 99999).$type;
		$path = './img/'. date('ymd');
		is_dir($path) || mkdir($path,0777,true);
		$newPath = $path . '/' . $fileName;
		move_uploaded_file($v['tmp_name'], $newPath);
		success('上传成功', 'Index.php');
	}
}
 ?>